﻿package game.objects {
	import game.network.Network;
	
	public class UserObject {
		
		public var id:Number			= 0;
		public var network:String		= Network.TEST;
		public var uid:String			= "";
		public var first_name:String	= "";
		public var last_name:String		= "";
		public var nickname:String		= "";
		public var photo:String			= "";
		public var sex:int				= 0;
		public var link:String			= "";
		public var level:int			= 0;
		public var exp:Number			= 0;
		public var autos:Vector.<AutoObject> = new Vector.<AutoObject>;
		
		public function applyApiUser(user:UserApiObject):void {
			this.uid			= user.uid;
			this.first_name		= user.first_name;
			this.last_name		= user.last_name;
			this.nickname		= user.nickname;
			this.photo			= user.photo;
			this.sex			= user.sex;
		}
		
		//возвращает машину по сid, если cid не указан то машина главная
		public function getAuto(cid:int):AutoObject {
			if (cid==-1)
				cid = cidMainAuto;
			return getAutoByCid(cid);
		}
		
		//возвращает машину по сid
		private function getAutoByCid(cid:int):AutoObject {
			var a:AutoObject;
			for each (var auto:AutoObject in autos) {
				if (auto.cid == cid) {
					a = auto;
					break;
				}
			}
			return a;
		}
		
		//возвращает сid главной машины конктретного пользователя
		private function get cidMainAuto():int {
			var i:int = -1;
			for each (var auto:AutoObject in autos) {
				if (auto.main == 1) {
					i = auto.cid;
					break;
				}
			}
			return i;
		}
		
		public function toString():String { return GameObject.formatToString(this, 'uid', 'first_name', 'last_name', 'nickname', 'photo', 'sex', 'level', 'exp'); }
		
	}
}